package com.eeepay.eeepay_v2.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.model.AgentProfitInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.view.ShareProfitDialog;
import com.eeepay.v2_library.adapter.ABBaseAdapter;
import com.eeepay.v2_library.adapter.ABViewHolder;

import java.util.List;

/**
 * Created by Ching on 2017/2/28.
 */

public class AddProfitAdapter extends ABBaseAdapter<AgentProfitInfo> {
    private List<AgentProfitInfo> list;
    private int index = -1;

    public AddProfitAdapter(Context context, List<AgentProfitInfo> list) {
        super(context);
        this.list = list;
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_agent_add_profit_listview;
    }

    @Override
    public void convert(final ABViewHolder holder, final AgentProfitInfo model) {
        holder.setRightText(R.id.lrt_add_profit_product, model.getBpName());//业务产品
        if (TextUtils.isEmpty(model.getProfitType()) || TextUtils.isEmpty(model.getPercent())) {
            holder.setTextAndColor(R.id.tv_profit_type, "", R.color.label_edittext_textColor);
            //设置类型和百分比
            holder.setTextAndColor(R.id.tv_profit_percent, "设置", R.color.label_edittext_textColor);
        } else {
            holder.setText(R.id.tv_profit_type, model.getProfitType());
            switch (model.getProfitType()) {
                case Constant.PROFIT_TYPE_1:
                    holder.setTextAndColor(R.id.tv_profit_percent, model.getPercent(), R.color.btn_blue);
                    break;
                case Constant.PROFIT_TYPE_2:
                case Constant.PROFIT_TYPE_3:
                    holder.setTextAndColor(R.id.tv_profit_percent, model.getPercent() + "%", R.color.btn_blue);
                    break;
            }
        }
        //dialog 设置
        holder.setViewOnClick(R.id.layout_card_profit, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(mContext,"设置刷卡分润",Toast.LENGTH_SHORT).show();
                final ShareProfitDialog dialog = new ShareProfitDialog(mContext);
                dialog.show();
                if (!TextUtils.isEmpty(model.getProfitType())) {
                    dialog.setProfitType(model.getProfitType());
                }
                dialog.setConfirmListener(new ShareProfitDialog.OnConfirmListener() {
                    @Override
                    public void onConfirm() {//设置成功回调
                        model.setPercent(dialog.getPercent());//百分比
                        model.setProfitType(dialog.getProfitType());//类型
                        switch (dialog.getProfitType()) {
                            case Constant.PROFIT_TYPE_1:
                                holder.setTextAndColor(R.id.tv_profit_percent, model.getPercent(), R.color.unify_bg);
                                break;
                            case Constant.PROFIT_TYPE_2:
                            case Constant.PROFIT_TYPE_3:
                                holder.setTextAndColor(R.id.tv_profit_percent, model.getPercent() + "%", R.color.unify_bg);
                                break;
                        }
                        holder.setText(R.id.tv_profit_type, model.getProfitType());//设置类型
                        list.set(holder.getPosition(), model);
                        dialog.dismiss();
                    }
                });
            }
        });

        final EditText editText = holder.getView(R.id.let_tx_profit);
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(8)});
        editText.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View view, MotionEvent event) {
                // 在TOUCH的UP事件中，要保存当前的行下标，因为弹出软键盘后，整个画面会被重画

                // 在getView方法的最后，要根据index和当前的行下标手动为EditText设置焦点

                if (event.getAction() == MotionEvent.ACTION_UP) {
                    index = holder.getPosition();
                }
                return false;
            }

        });
        editText.setTag(holder.getPosition());
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                int position = (int) editText.getTag();
                String temp = s.toString();
                int posDot = temp.indexOf(".");
                if (posDot > 0) {
                    if (temp.length() - posDot - 1 > 2) {
                        s.delete(posDot + 3, posDot + 4);
                    }
                }
                list.get(position).setProfit(s.toString().trim());
            }
        });

        editText.clearFocus();

        if (index != -1 && index == holder.getPosition()) {
            // 如果当前的行下标和点击事件中保存的index一致，手动为EditText设置焦点。
            editText.requestFocus();
        }
        //如果hashMap不为空，就设置的editText
        if (list.get(holder.getPosition()).getProfit() != null) {
            editText.setText(list.get(holder.getPosition()).getProfit());
        } else {
            editText.setText("");
        }
    }

}
